<template>
  <label 
    :class="[
      'bx-radio',
      {
        'bx-radio--checked': isChecked,
        'bx-radio--disabled': disabled
      }
    ]"
  >
    <span class="bx-radio__input">
      <input
        ref="inputRef"
        v-model="radioValue"
        type="radio"
        :disabled="disabled"
        :value="value"
        :name="name"
        class="bx-radio__original"
        @change="handleChange"
      />
      <span class="bx-radio__inner bx-glass">
        <span v-if="isChecked" class="bx-radio__dot"></span>
      </span>
    </span>
    <span v-if="$slots.default || label" class="bx-radio__label">
      <slot>{{ label }}</slot>
    </span>
  </label>
</template>

<script>
import { ref, computed, inject } from 'vue'

export default {
  name: 'BxRadio',
  props: {
    modelValue: {
      type: [String, Number, Boolean],
      default: ''
    },
    value: {
      type: [String, Number, Boolean],
      required: true
    },
    label: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    },
    name: {
      type: String,
      default: ''
    }
  },
  emits: ['update:modelValue', 'change'],
  setup(props, { emit }) {
    const inputRef = ref(null)
    const radioGroup = inject('bx-radio-group', null)

    const isGroup = computed(() => !!radioGroup)

    const radioValue = computed({
      get() {
        return isGroup.value ? radioGroup.modelValue : props.modelValue
      },
      set(value) {
        if (isGroup.value) {
          radioGroup.changeEvent(value)
        } else {
          emit('update:modelValue', value)
          emit('change', value)
        }
      }
    })

    const isChecked = computed(() => {
      return radioValue.value === props.value
    })

    const handleChange = (event) => {
      if (props.disabled) return
      
      const value = event.target.value
      radioValue.value = value
    }

    return {
      inputRef,
      radioValue,
      isChecked,
      handleChange
    }
  }
}
</script>

<style scoped>
.bx-radio {
  display: inline-flex;
  align-items: center;
  cursor: pointer;
  font-family: var(--bx-font-family);
  user-select: none;
  transition: all var(--bx-transition-medium);
}

.bx-radio:hover .bx-radio__inner {
  border-color: var(--bx-glass-border-hover);
  background: var(--bx-glass-bg-hover);
}

.bx-radio--disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.bx-radio__input {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.bx-radio__original {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
  margin: 0;
}

.bx-radio__inner {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border: 1px solid var(--bx-glass-border);
  border-radius: 50%;
  background: var(--bx-glass-bg);
  backdrop-filter: var(--bx-glass-blur);
  transition: all var(--bx-transition-medium);
}

.bx-radio--checked .bx-radio__inner {
  background: rgba(0, 122, 255, 0.25);
  border-color: rgba(0, 122, 255, 0.5);
}

.bx-radio__dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--bx-text-primary);
  opacity: 0;
  transform: scale(0);
  transition: all var(--bx-transition-fast);
}

.bx-radio--checked .bx-radio__dot {
  opacity: 1;
  transform: scale(1);
}

.bx-radio__label {
  margin-left: 8px;
  color: var(--bx-text-primary);
  font-size: 14px;
  line-height: 1.5;
}

.bx-radio--disabled .bx-radio__label {
  color: var(--bx-text-tertiary);
}

/* 焦点样式 */
.bx-radio__original:focus + .bx-radio__inner {
  box-shadow: 0 0 0 2px rgba(0, 122, 255, 0.1);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .bx-radio__inner {
    width: 16px;
    height: 16px;
  }
  
  .bx-radio__dot {
    width: 6px;
    height: 6px;
  }
  
  .bx-radio__label {
    font-size: 13px;
    margin-left: 6px;
  }
}
</style>

